package Lee_code;

import java.util.*;

public class Lee_451 {

    public String frequencySort(String s) {
        HashMap<Character, Integer> map = new HashMap<>();
        char[] s1 = s.toCharArray();
        for (char ch: s1)
        {
            map.put(ch, map.getOrDefault(ch, 0)+1);
        }
        char[] s2 = new char[s1.length];
        int sign = 0;

        ArrayList<Character> arrayList = new ArrayList<>(map.keySet());


        Collections.sort(arrayList, (a,b)->map.get(b) - map.get(a));

        for(char ch: arrayList)
        {
            for(int i=0; i< map.get(ch); i++)
            {
                s2[sign] = ch;
                sign++;
            }
        }
        return  new String(s2);
    }
    public static void main(String[] args) {

    }
}
